using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.CompositeUI.SmartParts;
using LibrErian.Model;
using Microsoft.Practices.ObjectBuilder;

namespace LibrErian.Modules.BasicBook
{
    [SmartPart]
    public partial class AuthorView : UserControl, IAuthorView
    {
        private AuthorViewPresenter presenter;

        [CreateNew]
        public AuthorViewPresenter Presenter
        {
            set
            {
                presenter = value;
                presenter.View = this;
            }
        }

        public AuthorView()
        {
            InitializeComponent();
        }

        public void SetAuthor(Author author)
        {
            authorBindingSource.DataSource = author;
        }

        private void AuthorView_Load(object sender, EventArgs e)
        {
            presenter.OnViewReady();
        }

        private bool IsValid()
        {
            if (string.IsNullOrEmpty(tbName.Text))
            {
                epValidation.SetError(tbName, "Name cannot be empty");
                return false;
            }
            return true;
        }

        private void tsbCancel_Click(object sender, EventArgs e)
        {
            presenter.OnCancel();
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            if (IsValid())
            {
                authorBindingSource.EndEdit();
                presenter.OnOk();
            }
        }
    }
}
